﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Client
{
    public partial class MainDlg : Form
    {
        #region Application Idle event
        [StructLayout(LayoutKind.Sequential)]
        public struct Msg
        {
            public IntPtr hWnd;
            public Message msg;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint time;
            public System.Drawing.Point p;
        }

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern bool PeekMessage(out Msg msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
        protected bool AppIsIdle()
        {
            Msg msg;
            return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
        }

        protected void OnApplicationIdle(object sender, EventArgs e)
        {
            while (AppIsIdle())
            {
                try
                {
                    while (this.Created) // This is our message loop
                    {
                        DirectX.Me.Render();
                        Application.DoEvents();
                    }
                }
                catch (Exception ex)
                {
                    // for debug..
                    Debug.WriteLine(ex.TargetSite.ToString());
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }
        #endregion


        public MainDlg()
        {
            InitializeComponent();
        }


        public static int posX = 330, posY = 330;

        Window DBackground;
        MirMsgBox MsgBox;

        private void TestCode()
        {
            DBackground = new Window(null);
            DBackground.Left = 0;
            DBackground.Top = 0;
            DBackground.Width = ClientGlobals.ScreenWidth;
            DBackground.Height = ClientGlobals.ScreenHeight;
            DBackground.Background = true;
            DBackground.Floating = false;
            DBackground.EnableFocus = false;
            DBackground.Visible = true;
            WinManager.Me.AddControl(DBackground, true);


            MsgBox = new MirMsgBox(DBackground);
            MsgBox.ShowMsgBox("This is a test.\\Line number two!", MsgBoxBtnType.Yes | MsgBoxBtnType.No);
            if (MsgBox.DialogResult == Client.DialogResult.No)
                Debug.WriteLine("No pressed...");

            DrawScreen.Me.AddSysMsg("test message 1");
            DrawScreen.Me.AddSysMsg("test message 2");
            DrawScreen.Me.AddSysMsg("test message 3");
            DrawScreen.Me.AddSysMsg("test message 4");
            DrawScreen.Me.AddSysMsg("test message 5");
            DrawScreen.Me.AddSysMsg("test message 6");
            DrawScreen.Me.AddSysMsg("test message 7");

            MirMap.Me.LoadMap("0", posX, posY);
            SceneManager.Me.ChangeScene(SceneType.PlayGame);
        }


        #region Form events
        void MainDlg_Load(object sender, EventArgs e)
        {
            Application.Idle += new EventHandler(OnApplicationIdle);    // Handle the idle event

            // Handle keyboard + mouse..
            this.KeyPress += new KeyPressEventHandler(MainDlg_KeyPress);
            this.KeyDown += new KeyEventHandler(MainDlg_KeyDown);
            this.MouseDown += new MouseEventHandler(MainDlg_MouseDown);
            this.MouseUp += new MouseEventHandler(MainDlg_MouseUp);
            this.MouseMove += new MouseEventHandler(MainDlg_MouseMove);
            
            // Prepare form for displaying
            this.ClientSize = new Size(ClientGlobals.ScreenWidth, ClientGlobals.ScreenHeight);
            this.Show();                                    // Shows The DirectX Form
            this.Activate();                                // Set The Client To Focused

            // Prepare DirectX
            DirectX.Me.WindowPositionX = this.Top;          // Set Window X Coordinate
            DirectX.Me.WindowPositionY = this.Left;         // Set Window Y Coordinate
            DirectX.Me.InitializeDirectX(this);             // Initialize DirectX
            DirectText.Me.LoadFonts();                      // Initialize Fonts

            ImageLib.InitializeImageLibraries();      // Initialize & Load WIL's

            DrawScreen.Me.Initialize();
            SceneManager.Me.Initializer();

            // Run some test codes..
            TestCode();
        }

        void MainDlg_FormClosed(object sender, FormClosedEventArgs e)
        {
            GC.Collect();
            ImageLib.UninitializeImageLibraries();
            DirectText.Me.Dispose();
            DirectX.Me.Dispose();
        }

        void MainDlg_MouseMove(object sender, MouseEventArgs e)
        {
            DrawScreen.Me.ShowHint(e.X, e.Y, "test hint\\to see if you work\\awesomeness :P", Color.Yellow, Color.Red, true);

            if (WinManager.Me.MouseMove(new Point(e.X, e.Y)) == true)
                return;

            SceneManager.Me.MouseMove(e);
        }

        void MainDlg_MouseDown(object sender, MouseEventArgs e)
        {
            if (WinManager.Me.MouseDown(new Point(e.X, e.Y)) == true)
                return;

            SceneManager.Me.MouseDown(e);
        }

        void MainDlg_MouseUp(object sender, MouseEventArgs e)
        {
            if (WinManager.Me.MouseUp(new Point(e.X, e.Y)) == true)
                return;
        }

        void MainDlg_KeyDown(object sender, KeyEventArgs e)
        {
            SceneManager.Me.KeyDown(e);
        }

        void MainDlg_KeyPress(object sender, KeyPressEventArgs e)
        {
            SceneManager.Me.KeyPress(e);
        }
        #endregion
    }
}
